#pragma once
#include "../tinyxml/tinyxml.h"
#include <vector>
#include <string>
#include <map>
using std::map;
using std::vector;
using std::string;

typedef enum
{
    FT_bool,
    FT_enum,
    FT_value,
}FieldType;
class CRegister;
class CRegItem
{
public:
    virtual string Value() { return "Unknown";}
    virtual BOOL isField(){return FALSE;}
};

class CField : public CRegItem
{
public:
    CField(CRegister* base):m_start(0),m_end(0),m_type(FT_enum),m_base(base){}
    ~CField(){}
    CField(const CField& rhs);
    CField& operator=(const CField& rhs);
    virtual BOOL    isField(){return TRUE;}
    BOOL isValue(){return FT_value == m_type ?TRUE :FALSE;  }
    bool Load(TiXmlElement* fieldEle);
    DWORD  setValue(DWORD index);
    DWORD  setValueAbs(DWORD val);
    DWORD getValue();
    int getIndex(DWORD value);
    virtual string Value();
    string ValueAbs();
    void UpdateValue(const char* val);
public:
    string m_name;
    string m_description;
    DWORD  m_start;
    DWORD  m_end;
    FieldType m_type;
    map<DWORD,string> m_values;
    vector<DWORD>   m_vlist;
    CRegister*      m_base;
};


class CRegister : public CRegItem
{
public:
    CRegister(void);
    CRegister(const CRegister& rhs);
    CRegister& operator=(const CRegister& rhs);
    bool Load(TiXmlElement* regEle);
    virtual string Value(){
        char tp[128];
        sprintf(tp,"%s - 0x%08X",m_name.c_str(),m_value);
        return tp;
    }
public:
    ~CRegister(void);
public:
    vector<CField>  m_fields;
    string          m_name;
    string          m_description;
    DWORD           m_address;
    DWORD           m_bitcount;
    DWORD           m_value;
};

class CRegList : public vector<CRegister>
{
public:
    CRegList(){}
    ~CRegList(){}
    CRegList(const char* fileName);
    bool Load(const char* fileName);
};




